using System;
using System.Collections.Generic;
using System.Text;

namespace MicrosoftFrance.MCS.EPM2007
{
    class PSIHelper
    {
        public static Guid CheckOutProject(Guid projID) {
            Guid sessionGuid = Guid.NewGuid();
            try
            {
                Program.project.CheckOutProject(projID, sessionGuid, "Checked out by Program to import data");
            }
            catch (Exception ee)
            {
                throw ee;
            }
            return sessionGuid;
        }

        public static void CheckInProject(Guid projID, Guid sessionID)
        {
            //checkin project
            Guid jobID = Guid.NewGuid();
            Program.project.QueueCheckInProject(jobID, projID, true, sessionID, "Checked in");
        }

        public static void AddToProjectFull(Guid prjID, ProjectWS.ProjectDataSet projectDS)
        {
            Guid sessionGuid = CheckOutProject(prjID);
            AddToProjectInSession(sessionGuid, projectDS);
            CheckInProject(prjID, sessionGuid);
        }

        public static void AddToProjectInSession(Guid sessionID, ProjectWS.ProjectDataSet projectDS)
        {
            Guid jobGuid = Guid.NewGuid();
            try
            {
                Program.project.QueueAddToProject(jobGuid, sessionID, projectDS, false);
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }

        public static void UpdateProjectFull(Guid prjID, ProjectWS.ProjectDataSet projectDS)
        {
            Guid sessionGuid = CheckOutProject(prjID);
            UpdateProjectInSession(sessionGuid, projectDS);
            CheckInProject(prjID, sessionGuid);
        }

        public static void UpdateProjectInSession(Guid sessionID, ProjectWS.ProjectDataSet projectDS)
        {
            Guid jobGuid = Guid.NewGuid();
            try
            {
                Program.project.QueueUpdateProject(jobGuid, sessionID, projectDS, false);
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }

        public static void Publish(Guid[] projectGuids, bool fullPublish)
        {
            Guid jobGuid = Guid.NewGuid();
            for (int i = 0; i < projectGuids.Length; i++)
            {
                jobGuid = Guid.NewGuid();

                Program.project.QueuePublish(jobGuid, projectGuids[i], fullPublish, "");
            }
        }

    }
}
